package controllers

import (
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"novel-test/cache"
	"novel-test/models"
	"novel-test/pkg"
	"strconv"
	"time"
)

type UserController struct {
	beego.Controller
}

type Res struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func (c *UserController) Login() {
	username := c.GetString("username")
	password := c.GetString("password")

	if username == "" || password == "" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "用户或密码为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	login := "login_key" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, login).Int()
	if errCount >= 3 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "密码错误3次，禁止登录",
		}
		c.ServeJSON()
		return
	}
	var err error
	var user models.User
	err = models.DB.Model(&models.User{}).Where("username = ?", username).Find(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "用户查询失败",
		}
		c.ServeJSON()
		return
	}
	if user.ID == 0 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "用户不存在",
		}
		c.ServeJSON()
		return
	}
	if user.Password != pkg.Md5Str(password) {
		cache.Rdb.Incr(cache.Ctx, login)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, login, 3, time.Hour*3)
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "密码错误3次，封禁3小时",
			}
			c.ServeJSON()
			return
		}
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "密码错误",
		}
		c.ServeJSON()
		return

	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "登录成功",
	}
	c.ServeJSON()
	return

}
func (c *UserController) List() {
	dataList := "data_list"
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataList).Result()
	var user []models.User
	if cacheJson == "" {
		models.DB.Model(&models.User{}).Find(&user)
		dataJson, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, dataList, dataJson, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &user)
		if err != nil {
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "缓存数据获取失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		}
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "缓存数据获取成功",
		Data: user,
	}
	c.ServeJSON()
	return

}
func (c *UserController) Detail() {
	id, _ := c.GetInt("id")
	dataList := "data_list" + strconv.Itoa(id)
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataList).Result()
	var user models.User
	if cacheJson == "" {
		models.DB.Model(&models.User{}).Where("id = ?", id).First(&user)
		dataJson, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, dataList, dataJson, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &user)
		if err != nil {
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "缓存数据获取失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		}
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "缓存数据获取成功",
		Data: user,
	}
	c.ServeJSON()
	return

}
func (c *UserController) RoleNodeAdd() {
	rid, _ := c.GetInt("rid", 0) //角色id
	nid, _ := c.GetInt("nid", 0) //权限id
	if rid == 0 && nid == 0 {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "角色ID 或者 权限ID 为空",
		}
		c.ServeJSON()
		return
	}

	data := models.RoleNodeRelation{
		Nid: nid,
		Rid: rid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.Data["json"] = map[string]interface{}{
			"code": 500,
			"msg":  "添加失败",
		}
		c.ServeJSON()
		return
	}

	c.Data["json"] = map[string]interface{}{
		"code": 200,
		"msg":  "添加成功",
	}
	c.ServeJSON()
	return

}

//	func (c *UserController) UserRole() {
//		uid, _ := c.GetInt("id")
//		if uid == 0 {
//			c.Data["json"] = Res{
//				Code: 0,
//				Msg:  "用户uid为空",
//			}
//			c.ServeJSON()
//			return
//		}
//		models.DB.Model(models.Role{}).
//			Joins("left join user_role_relations on user_role_relations.rid = roles.id").
//			Where("user_role_")
//	}
func (c *UserController) LoginTest() {
	username := c.GetString("username")
	password := c.GetString("password")

	if username == "" || password == "" {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "用户或者密码为空",
		}
		c.ServeJSON()
		return
	}
	login := "login_key" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, login).Int()
	if errCount >= 3 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "密码错误大于3次，登录封禁",
		}
		c.ServeJSON()
		return
	}
	var err error
	var user models.User
	err = models.DB.Model(&models.User{}).Where("username = ?", username).Find(&user).Error
	if err != nil {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "用户查询失败",
		}
		c.ServeJSON()
		return
	}
	if user.ID == 0 {
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "用户不存在",
		}
		c.ServeJSON()
		return
	}
	if user.Password != pkg.Md5Str(password) {
		cache.Rdb.Incr(cache.Ctx, login)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, login, 3, time.Hour*2)
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "密码错误3次以上,封禁3小时",
			}
			c.ServeJSON()
			return
		}
		c.Data["json"] = Res{
			Code: 0,
			Msg:  "密码错误",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "登录成功",
	}
	c.ServeJSON()

}
func (c *UserController) ListTest() {
	dataList := "data_list"
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataList).Result()
	var user []models.User
	if cacheJson == "" {
		models.DB.Model(&models.User{}).Find(&user)
		dataJson, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, dataList, dataJson, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &user)
		if err != nil {
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "缓存数据查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		}
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "缓存数据查询成功",
		Data: user,
	}
	c.ServeJSON()
	return
}
func (c *UserController) DetailTest() {
	id, _ := c.GetInt("id")
	dataListKey := "data_key" + strconv.Itoa(id)
	cacheJson, _ := cache.Rdb.Get(cache.Ctx, dataListKey).Result()
	var user models.User
	if cacheJson == "" {
		models.DB.Model(&models.User{}).Where("id = ?", id).First(&user)
		dataJson, _ := json.Marshal(user)
		cache.Rdb.Set(cache.Ctx, dataListKey, dataJson, time.Hour*2)
	} else {
		err := json.Unmarshal([]byte(cacheJson), &user)
		if err != nil {
			c.Data["json"] = Res{
				Code: 0,
				Msg:  "缓存数据查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		}
	}
	c.Data["json"] = Res{
		Code: 200,
		Msg:  "缓存数据查询成功",
		Data: user,
	}
	c.ServeJSON()
}
